package com.example.broadcastrecevicedemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private DynamiReceiver receiver;
    private  SecondBroad secondBroad;
    private ThirdBroad thirdBroad;
    private LoaclBroad loaclBroad;
    private LocalBroadcastManager localBroadcastManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBroad();
    }

    @Override
    protected void onResume() {
        super.onResume();
        receiver=new DynamiReceiver();
        IntentFilter intentFilter=new IntentFilter();
        IntentFilter intentFilter1=new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        intentFilter.addAction("com.example.BroadcastRecevice.NORMAL");

        secondBroad=new SecondBroad();
        thirdBroad=new ThirdBroad();
        intentFilter.addAction("com.example.BroadcastRecevice.ORDERED");
        intentFilter1.addAction("com.example.BroadcastRecevice.ORDERED");
        intentFilter.setPriority(-100);
        intentFilter1.setPriority(100);
        registerReceiver(secondBroad,intentFilter);
        registerReceiver(thirdBroad,intentFilter1);

        loaclBroad=new LoaclBroad();
        IntentFilter intentFilter2=new IntentFilter();
        localBroadcastManager=LocalBroadcastManager.getInstance(this);
        intentFilter2.addAction("com.example.BroadcastRecevice.LOCAL");
        localBroadcastManager.registerReceiver(loaclBroad,intentFilter2);

    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
        unregisterReceiver(secondBroad);
        unregisterReceiver(thirdBroad);
        localBroadcastManager.unregisterReceiver(loaclBroad);
    }
  private void initBroad(){
      Button button=findViewById(R.id.send_normal_broad);
      Button button1=findViewById(R.id.order_broad);
      Button button2=findViewById(R.id.local_broad);
      button2.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent intent=new Intent();
              intent.setAction("com.example.BroadcastRecevice.LOCAL");
              intent.putExtra("extra_text","来自发送广播者的问候");
              localBroadcastManager.sendBroadcast(intent);
          }
      });
      button1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent intent=new Intent();
              intent.setAction("com.example.BroadcastRecevice.ORDERED");
              intent.putExtra("extra_text","来自发送广播者的问候");
              sendBroadcast(intent);
          }
      });
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent intent=new Intent();
              intent.setAction("com.example.BroadcastRecevice.NORMAL");
              intent.putExtra("extra_text","来自发送广播者的问候");
              sendBroadcast(intent);
          }
      });

  }
  class SecondBroad extends BroadcastReceiver{
      private final String TAG = "tag";
      @Override
      public void onReceive(Context context, Intent intent) {

          String action=intent.getAction();
          switch (action){
              case "com.example.BroadcastRecevice.ORDERED":
                  String text=intent.getStringExtra("extra_text");
                  Log.d(TAG,"second广播接收器收到："+text);
                  break;
          }
      }
  }
  class ThirdBroad extends BroadcastReceiver{
      private final String TAG = "tag";
      @Override
      public void onReceive(Context context, Intent intent) {
          String action=intent.getAction();
          switch (action){
              case "com.example.BroadcastRecevice.ORDERED":
                  String text=intent.getStringExtra("extra_text");
                  Log.d(TAG,"third广播接收器收到："+text);
                  /*text+="修改";
                  intent.putExtra("extra_text",text);*/
                  //拦截
                  //abortBroadcast();
                  break;
          }
      }
  }

  class LoaclBroad extends BroadcastReceiver{
      private final String TAG = "tag";
      @Override
      public void onReceive(Context context, Intent intent) {
          String action=intent.getAction();
          switch (action){
              case "com.example.BroadcastRecevice.LOCAL":
                  String text=intent.getStringExtra("extra_text");
                  Log.d(TAG,"应用内广播接收器收到："+text);
                  break;
          }
      }
  }

}
